﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BaocaoNPC.Usercontrols.reports;
using System.Data.OleDb;
using BaocaoNPC.common;
using Component;
using CMISLibrary;
using bus_TienichNPC;

namespace BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for DS_THUTIEN_ATM.xaml
    /// </summary>
    public partial class DS_THUTIEN_ATM : UserControl
    {
        public DataSet ds_DM_NH = new DataSet();
        public DataSet ds_DM_PTTT = new DataSet();
        public DataSet ds_DM_TNGAN = new DataSet();
        public DataSet ds_KHNOPTIEN = new DataSet();
        public DataSet ds_KHNOPTIEN_NH = new DataSet();
        public DataSet ds_KHNOPTIEN_NH_All = new DataSet();
        public DataSet ds_KHNOPTIEN_NPC = new DataSet();
        public DataSet ds_KHNOPTIEN_NPC_All = new DataSet();
        public DataSet ds_KHNOPTIEN_KHOP = new DataSet();
        //public DataSet ds_KHLECH = new DataSet();
        public long pcount = 0;//so cong to duoc chon
        public long ptcount = 0;// tong so cong to load ra luoi
        public long ptien = 0;
        public Int16 i16ThangHT = 0;
        public Int16 i16NamHT = 0;
        public Int16 i16Hour = 16;
        public Int16 i16Munite = 30;
        public int int_ttrang_no = 0;
        public DateTime? ptungay, pdenngay;
        public string strmanh,strmapttt;        
        public String strMadviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";
        public string strUsername = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "NPC_ATM";

        public DS_THUTIEN_ATM()
        {
            try
            {
                InitializeComponent();
                Component.TabIndexManager.RegisterTabManagerWithSelectAll(this);
                ds_DM_NH = GlobalServices.Service_NPCUS.get_DM_NHANG();
                ds_DM_PTTT = GlobalServices.Service_NPCUS.get_PHTHUC_TTOAN();
                ds_DM_TNGAN = GlobalServices.Service_NPCUS.get_DM_TNGAN(strMadviqly);
                txtchonNGHANG.AddItem( new CMIS2_TextBox.AutoCompleteEntry(ds_DM_NH));
                txtpthucttoan.AddItem(new CMIS2_TextBox.AutoCompleteEntry(ds_DM_PTTT));
                txtthungan.AddItem(new CMIS2_TextBox.AutoCompleteEntry(ds_DM_TNGAN));
                dtpngaynop_den.SelectedDate = DateTime.Now;
                dtpngaynop_tu.SelectedDate = DateTime.Now;
                dtpngaynopdoichieu_den.SelectedDate = DateTime.Now;
                dtpngaynopdoichieu_tu.SelectedDate = DateTime.Now;
                cbotinhtrang_no.SelectedIndex = 0;
                GET_THANGNAM_HT();
                GET_TIMESPAN();
                txthour.Text = i16Hour.ToString();
                txtmunite.Text = i16Munite.ToString();
                txthour1.Text = i16Hour.ToString();
                txtmunite1.Text = i16Munite.ToString();
                set_cbotinhtrang_no();
                btnGhi.IsEnabled = false;
                btnDelete.IsEnabled = false;
                if (i16ThangHT.ToString().Length == 1)
                {
                    dtpThangNamHT.Text = "0" + i16ThangHT.ToString() + "/" + i16NamHT.ToString();
                }
                else
                {
                    dtpThangNamHT.Text = i16ThangHT.ToString() + "/" + i16NamHT.ToString(); 
                }
               
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }
        private void GET_THANGNAM_HT()
        {
            try
            {
                DataSet dsthanglviec = new DataSet();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //dsthanglviec = bus.get_thang_lviec();
                dsthanglviec = GlobalServices.Service_NPCUS.get_thang_lviec();
                if (dsthanglviec != null && dsthanglviec.Tables.Count > 0)
                {
                    foreach (DataRow dr in dsthanglviec.Tables[0].Rows)
                        if (dr["PHAN_HE"].ToString() == "CN" && dr["MA_DVIQLY"].ToString() == strMadviqly)
                        {
                            i16ThangHT = Convert.ToInt16(dr["THANG"]);
                            i16NamHT = Convert.ToInt16(dr["NAM"]);
                        }
                }

            }
            catch
            {
                i16ThangHT = Convert.ToInt16(DateTime.Now.Month);
                i16NamHT = Convert.ToInt16(DateTime.Now.Year);
            }
        }
        private void GET_TIMESPAN()
        {
            try
            {
                DataSet dstime = new DataSet();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //dsthanglviec = bus.get_thang_lviec();
                dstime = GlobalServices.Service_NPCUS.get_TIMESPAN(strMadviqly);
                if (dstime != null && dstime.Tables.Count > 0)
                {

                    i16Hour = Convert.ToInt16(dstime.Tables[0].Rows[0]["GIO"]);
                    i16Munite = Convert.ToInt16(dstime.Tables[0].Rows[0]["PHUT"]);      
                }

            }
            catch
            {
                i16Hour = 16;
                i16Munite = 30;
            }
        }
        private void set_cbotinhtrang_no()
        {
            cbotinhtrang_no.Items.Clear();
            cbotinhtrang_no.Items.Add("Chưa chấm");
            cbotinhtrang_no.Items.Add("Đã chấm");
            cbotinhtrang_no.Items.Add("Tất cả");
        }

        private void btnload_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.Wait;
                int id1 = txtpthucttoan.Text.IndexOf("-");
                int id2 = txtchonNGHANG.Text.IndexOf("-");
                if (id1 == -1)
                { strmapttt = ""; }
                else
                {
                    strmapttt = txtpthucttoan.Text.Substring(0, id1);
                }
                if (id2 == -1)
                { strmanh = ""; }
                else
                {
                    strmanh = txtchonNGHANG.Text.Substring(0, id2);
                }
                //System.Globalization.DateTimeFormatInfo sFormat = new System.Globalization.DateTimeFormatInfo();
                //sFormat.ShortDatePattern = "dd/MM/yyyy";

                ptungay = dtpngaynop_tu.SelectedDate;
                pdenngay = dtpngaynop_den.SelectedDate;

                #region checkngay
                if (pdenngay == null)
                    showMessErrorOnView("Điều kiện ngày nộp không được để trống", dtpngaynop_den);
                if (ptungay == null)
                    showMessErrorOnView("Điều kiện ngày nộp không được để trống", dtpngaynop_tu);
                if (ptungay > pdenngay)
                    showMessErrorOnView("Điều kiện ngày nộp không hợp lệ", dtpngaynop_tu);
                #endregion

                TimeSpan ts = new TimeSpan(i16Hour, i16Munite, 0);
                ptungay = ptungay.Value.AddDays(-1) + ts;
                pdenngay = pdenngay + ts;
                
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //ds_KHNOPTIEN = bus.get_dsKH_thuATM(strMadviqly, strmanh, strmapttt, ptungay, pdenngay, int_ttrang_no);
                ds_KHNOPTIEN = GlobalServices.Service_NPCUS.get_dsKH_thuATM(strMadviqly, strmanh, strmapttt, ptungay, pdenngay, int_ttrang_no);


                if (ds_KHNOPTIEN != null)
                    if (ds_KHNOPTIEN.Tables.Count > 0)
                        if (ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count > 0)
                        {
                            dgrdsthutien.ItemsSource = ds_KHNOPTIEN.Tables[0].DefaultView;
                            ds_KHNOPTIEN.Tables[0].DefaultView.AllowNew = false;
                            if (int_ttrang_no == 0)
                            {
                                this.btnGhi.IsEnabled = true;
                                btnDelete.IsEnabled = false;
                            }
                            if (int_ttrang_no == 1)
                            {
                                this.btnGhi.IsEnabled = false;
                                btnDelete.IsEnabled = true;
                            }
                            if (int_ttrang_no == 2)
                            {
                                this.btnGhi.IsEnabled = false;
                                btnDelete.IsEnabled = false;
                            }
                        }
                        else
                        { showMessErrorOnView("Không tìm thấy thông tin theo điều kiện chọn", dtpngaynop_tu); return; }
                    else { showMessErrorOnView("Không tìm thấy thông tin theo điều kiện chọn", dtpngaynop_tu); return; }
                else { showMessErrorOnView("Không tìm thấy thông tin theo điều kiện chọn", dtpngaynop_tu); return; }
                for (Int32 i = 0; i < ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count; i++)
                {
                    ptien = ptien + Convert.ToInt64(ds_KHNOPTIEN.Tables["DSTHU"].Rows[i]["TTIEN_NOP"]);
                }
                ptcount = ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count;
                pcount = ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count;
                lblTongSo.Content = "Tổng số:" + pcount.ToString() + "/" + ptcount;
                lblTongtien.Content = "Tổng tiền:" + ptien;
                btnHuy.IsEnabled = true;
                btnin.IsEnabled = true;
                
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
            this.Cursor = Cursors.Arrow;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            btnGhi.IsEnabled = false;
            btnHuy.IsEnabled = false;
            btnin.IsEnabled = false;
            btnload.IsEnabled = true;
            btnindoisoat.IsEnabled = false;
            btnDoisoat.IsEnabled = true;
        }

        private void btnHuy_Click(object sender, RoutedEventArgs e)
        {
            ds_KHNOPTIEN.Clear();
            btnGhi.IsEnabled = false;
            btnin.IsEnabled = false;
            btnHuy.IsEnabled = false;
            btnload.IsEnabled = true;
        }

        private void btnGhi_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                DateTime ptungay = dtpngaynop_tu.SelectedDate.Value.ToShortDateString() == string.Empty ? DateTime.Now : Convert.ToDateTime(dtpngaynop_tu.SelectedDate.Value.ToShortDateString());
                //int id3 = txtthungan.Text.IndexOf("-");
                //if (id3 == -1)
                //{ strMatngan = ""; }
                //else
                //{
                //    strMatngan = txtthungan.Text.Substring(0,id3);
                //}
                string ERRO = "";
                string lstErro = "";

                DataSet dsSelect = new DataSet();
                DataSet dsErro = new DataSet();
                DataTable dtSelect = ds_KHNOPTIEN.Tables["DSTHU"].Clone();
                DataTable dtErro = new DataTable();
                dtErro.Columns.Add("MA_DVIQLY");
                dtErro.Columns.Add("MA_GD");
                dtErro.Columns.Add("ID_HDON");
                dtErro.Columns.Add("LOAI_HDON");
                dtErro.Columns.Add("MA_ERRO");
                DataView drv = dgrdsthutien.ItemsSource as DataView;
                DataRow[] drselect = drv.Table.Select("CHON=1");
                if (drselect.Length > 0)
                {
                    if (MessageBox.Show("Bạn có muốn cập nhật chấm nợ cho những hóa đơn đã được chọn?", "Thông báo", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                        foreach (DataRow dr in drselect)
                        {
                            if (Convert.ToInt64(dr["TIEN_NO"].ToString()) == Convert.ToInt64(dr["SO_TIEN"].ToString()) && Convert.ToInt16(dr["TTRANG_SSAI"].ToString()) == 0 && Convert.ToInt16(dr["TTHAI_GIAO"].ToString()) != 0)
                            {
                                //Can bo xung kiem tra ngay nop && thang HT
                                dtSelect.ImportRow(dr);
                            }
                            else if (Convert.ToInt64(dr["TIEN_NO"].ToString()) == 0 && Convert.ToInt64(dr["SO_TIEN"].ToString()) != 0)
                            {
                                DataRow drerr = dtErro.NewRow();
                                drerr.SetField("MA_DVIQLY", dr["MA_DVIQLY"]);
                                drerr.SetField("MA_GD", dr["MA_GD"]);
                                drerr.SetField("ID_HDON", dr["ID_HDON"]);
                                drerr.SetField("LOAI_HDON", dr["LOAI_HDON"]);
                                drerr.SetField("MA_ERRO", "1");//hóa đơn đã được chấm
                                dtErro.Rows.Add(drerr);
                                lstErro = lstErro + ";" + dr["ID_HDON"].ToString();
                            }
                            else if (Convert.ToInt64(dr["TIEN_NO"].ToString()) != 0 && Convert.ToInt64(dr["SO_TIEN"].ToString()) != Convert.ToInt64(dr["TIEN_NO"].ToString()))
                            {
                                DataRow drerr = dtErro.NewRow();
                                drerr.SetField("MA_DVIQLY", dr["MA_DVIQLY"]);
                                drerr.SetField("MA_GD", dr["MA_GD"]);
                                drerr.SetField("ID_HDON", dr["ID_HDON"]);
                                drerr.SetField("LOAI_HDON", dr["LOAI_HDON"]);
                                drerr.SetField("MA_ERRO", "2");//Tiền thu khác số tiền nợ
                                dtErro.Rows.Add(drerr);
                                lstErro = lstErro + ";" + dr["ID_HDON"].ToString();
                            }
                            else if (Convert.ToInt64(dr["TIEN_NO"].ToString()) != 0 && Convert.ToInt32(dr["TTRANG_SSAI"].ToString()) == 1)
                            {
                                DataRow drerr = dtErro.NewRow();
                                drerr.SetField("MA_DVIQLY", dr["MA_DVIQLY"]);
                                drerr.SetField("MA_GD", dr["MA_GD"]);
                                drerr.SetField("ID_HDON", dr["ID_HDON"]);
                                drerr.SetField("LOAI_HDON", dr["LOAI_HDON"]);
                                drerr.SetField("MA_ERRO", "3");
                                dtErro.Rows.Add(drerr);//Hóa đơn đã được tiếp nhận sửa sai
                                lstErro = lstErro + ";" + dr["ID_HDON"].ToString();
                            }
                            else if (Convert.ToInt64(dr["TIEN_NO"].ToString()) != 0 && Convert.ToInt16(dr["TTHAI_GIAO"].ToString()) == 0)
                            {
                                DataRow drerr = dtErro.NewRow();
                                drerr.SetField("MA_DVIQLY", dr["MA_DVIQLY"]);
                                drerr.SetField("MA_GD", dr["MA_GD"]);
                                drerr.SetField("ID_HDON", dr["ID_HDON"]);
                                drerr.SetField("LOAI_HDON", dr["LOAI_HDON"]);
                                drerr.SetField("MA_ERRO", "4");
                                dtErro.Rows.Add(drerr);//Hóa đơn chưa được giao
                                lstErro = lstErro + ";" + dr["ID_HDON"].ToString();
                            }
                        }
                        dtErro.AcceptChanges();
                        dtErro.TableName = "ERRO";
                        dsErro.Tables.Add(dtErro);
                        dtSelect.AcceptChanges();
                        dtSelect.TableName = "DSCHAMNO";
                        dsSelect.Tables.Add(dtSelect);
                        //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                        //if( bus.chamno_ATM(dsSelect,strMatngan,ptungay))
                        if (dtSelect.Rows.Count > 0)
                        {
                            //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                            //if (bus.chamno_ATM(dsSelect, ptungay, strUsername, ref ERRO))
                            if (GlobalServices.Service_NPCUS.chamno_ATM(dsSelect, ptungay, strUsername, ref ERRO))
                            {
                                showMessSucOnView("Cập nhật chấm nợ thành công.");
                                loadgrid();
                                btnGhi.IsEnabled = false;
                            }
                            else
                            {

                                showMessErrorOnView("Lỗi trong quá trình chấm nợ");
                                GlobalServices.Service_NPCUS.Insert_LogChamno(dsSelect, strUsername, "4", 0, "INS", ref ERRO);
                                txtthungan.Focus();
                                return;
                            }
                        }
                        if (dtErro.Rows.Count > 0)
                        {
                            Int64 icount = dtErro.Rows.Count;
                            MessageBox.Show("Có " + icount.ToString() + "hóa đơn không chấm nợ được:" + lstErro);
                            //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                            //bus.update_chamno_atm_ERRO(dsErro, strUsername, ref ERRO);
                            GlobalServices.Service_NPCUS.update_chamno_atm_ERRO(dsErro, strUsername, ref ERRO);
                        }
                    }
                }
                else
                {
                    showMessErrorOnView("Bạn chưa chọn hóa đơn nào để chấm"); return;
                }
            }
            catch (Exception ex)
            { showMessErrorOnView(ex.ToString()); }
        }

        private void chkDaochon_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataView drv = dgrdsthutien.ItemsSource as DataView;
                DataRow[] drselect = drv.Table.Select("CHON=1");
                pcount = drselect.Count();
                DataRow[] drunselect = drv.Table.Select("CHON=0");
                foreach (DataRow dr in drselect)
                {
                    dr["CHON"] = 0;
                    pcount = pcount - 1;
                    ptien = ptien - Convert.ToInt64(dr["TTIEN_NOP"]);
                }
                foreach (DataRow dr in drunselect)
                {
                    dr["CHON"] = 1;
                    pcount = pcount + 1;
                    ptien = ptien + Convert.ToInt64(dr["TTIEN_NOP"]);
                }
                
                lblTongSo.Content = "Tổng số:" + pcount.ToString() + "/" + ptcount;
                lblTongtien.Content = "Tổng tiền:" + ptien;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }

        }

        private void chkChontat_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                DataView drv = dgrdsthutien.ItemsSource as DataView;
                DataRow[] drselect = drv.Table.Select("CHON=0");
                foreach (DataRow dr in drselect)
                {
                    dr["CHON"] = 1;
                    ptien = ptien + Convert.ToInt64(dr["TTIEN_NOP"]);
                }
                pcount = drv.Count;
                lblTongSo.Content = "Tổng số:" + pcount.ToString() + "/" + ptcount;
                lblTongtien.Content = "Tổng tiền:" + ptien;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void chkChontat_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                DataView drv = dgrdsthutien.ItemsSource as DataView;
                DataRow[] drselect = drv.Table.Select("CHON=1");
                foreach (DataRow dr in drselect)
                {
                    dr["CHON"] = 0;
                }
                pcount = 0;
                ptien = 0;
                lblTongSo.Content = "Tổng số:" + pcount.ToString() + "/" + ptcount;
                lblTongtien.Content = "Tổng tiền:" + ptien;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void dgrDSDDO_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                DataRowView drv_khnoptien = dgrdsthutien.SelectedItem as DataRowView;
                if (drv_khnoptien == null)
                { return; }
                if (dgrdsthutien.CurrentCell.Column == null)
                { return; }
                if (drv_khnoptien != null)
                {
                    if (dgrdsthutien.CurrentCell.Column.HeaderStringFormat.ToUpper() == "CHON")
                    {
                        if (Convert.ToBoolean(drv_khnoptien["CHON"]) == false)
                        {
                            pcount = pcount - 1;
                            ptien = ptien - Convert.ToInt64(drv_khnoptien["TTIEN_NOP"].ToString());
                            //DataView dv = dgrdsthutien.ItemsSource as DataView;
                            //DataRow[] arrSelect = dv.Table.Select("CHON = 1");
                            //DataRow[] arrUnSelect = dv.Table.Select("CHON = 0");
                            //pcount = arrSelect.Length;
                            //ptien = 0;
                            //for (Int32 i = 0; i < arrSelect.Length; i++)
                            //{ ptien = ptien + Convert.ToInt64(arrSelect[i]["TTIEN_NOP"]); }
                            //for (Int32 j = 0; j < arrUnSelect.Length; j++)
                            //{ ptien = ptien - Convert.ToInt64(arrUnSelect[j]["TTIEN_NOP"]); }
                        }
                        else
                        {
                            pcount = pcount + 1;
                            ptien = ptien + Convert.ToInt64(drv_khnoptien["TTIEN_NOP"].ToString());
                        }
                    }
                    lblTongSo.Content = "Tổng số:" + pcount.ToString() + "/" + ptcount;
                    lblTongtien.Content = "Tổng tiền:" + ptien;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }
       
        public void showMessErrorOnView(string str)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 15000, str, Component.Validate.ValidationManager.MESSEGER_TYPE.ERROR_MESSAGE);
        }

        public void showMessSucOnView(string str)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 15000, str, Component.Validate.ValidationManager.MESSEGER_TYPE.SUCCESS_MESSAGE);
        }

        public void showMessErrorOnView(string str, object errorObjec)
        {
            Component.Validate.ValidationManager.showMessOnView(this, errorObjec, str, Component.Validate.ValidationManager.MESSEGER_TYPE.ERROR_MESSAGE);
        }

        private void btnin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //ds_KHNOPTIEN.WriteXmlSchema("D:\\Projects\\NPC_CMIS2\\NPC_CMIS2\\NPC_TIENICH\\Usercontrols\\DSKHNOPTIEN.xsd");
                rptDsThutien rpt = new rptDsThutien();
                rpt.SetDataSource(ds_KHNOPTIEN.Tables["DSTHU"]);
                rpt.SetParameterValue("pdenngay", pdenngay);
                rpt.SetParameterValue("ptungay", ptungay);
                UtilReportOMS.RptManager.showReportToWin(this, rpt, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "NPC-ATM");
                
            }
            catch (Exception ex)
            {showMessErrorOnView(ex.ToString()); return; }
        }

        private void btnTimFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.Filter = "Microsoft Excel (.xls) OpenOffice (.ods)|*.xls;*.ods";
                Nullable<bool> result = dlg.ShowDialog();
                if (result == true)
                {
                    string sfiledoisoat = "";
                    string path = dlg.FileName.Trim();
                    
                    if ((!string.IsNullOrEmpty(path)))
                    {
                        txtchonfile.Text = path;

                        btnTimFile.IsEnabled = true;
                        btnTimFile.Focus();
                    }
                    string[] filenames = path.Split('\\');
                    foreach (string sfilename in filenames)
                    {
                        if (sfilename.Contains(".xls"))
                            sfiledoisoat = sfilename;
                    }
                }
            }
            catch
            { return; }
        }
        public DataSet ShowFileExcel(string strDir)
        {
            try
            {

                DataSet ds = new DataSet();
                String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDir + ";" + "Extended Properties=Excel 8.0;";
                OleDbConnection objConn = new OleDbConnection(sConnectionString);
                if (objConn.State == ConnectionState.Closed)
                {
                    objConn.Open();
                }
                List Sheetlist = new List();
                DataTable dtxls = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                if (dtxls == null || dtxls.Rows.Count < 1)
                {
                    throw new Exception("Error: Could not determine the name of the first worksheet.");
                }
                string firstSheetName = dtxls.Rows[0]["TABLE_NAME"].ToString();
                //firstSheetName = firstSheetName.Substring(0,firstSheetName.IndexOf("$"));

                OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + firstSheetName + "] ORDER by F4", objConn);
                //OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [VIB_Bill_Payment_Succcess$]" , objConn);
                OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
                objAdapter1.SelectCommand = objCmdSelect;
                objAdapter1.Fill(ds);
                objConn.Close();
                //}
                return ds;
                string str = strDir.Replace(@"\", "/");
                //strFileNameError = str.Remove(0, str.LastIndexOf('/') + 1);
            }
            catch (Exception ex)
            {
                return null; //lblError.Foreground = Brushes.Red; lblError.Content = "Lỗi khi đọc file: " + ex.Message;
            }
        }

        private void btnindoisoat_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if(cboloaidsoat.SelectedIndex==0)//in nhung hoa don lech sau doi soat
                {
                if (ds_KHNOPTIEN_NH != null)
                {
                    if (ds_KHNOPTIEN_NH.Tables[0].Rows.Count > 0 || ds_KHNOPTIEN_NPC.Tables["DSTHU"].Rows.Count>0)
                    {
                        //ds_KHNOPTIEN_NH.WriteXmlSchema("D:\\Projects\\NPC_CMIS2\\NPC_CMIS2\\NPC_TIENICH\\Usercontrols\\DSDOISOAT_NHANG.xsd");
                        rptDoisoatNganhang rpt = new rptDoisoatNganhang();
                        rpt.Subreports[1].SetDataSource(ds_KHNOPTIEN_NPC.Tables["DSTHU"]);
                        rpt.Subreports[0].SetDataSource(ds_KHNOPTIEN_NH.Tables[0]);
                        rpt.SetParameterValue("pdenngay", pdenngay);
                        rpt.SetParameterValue("ptungay", ptungay);
                        rpt.SetParameterValue("loaidoichieu", "(Danh sách những hóa đơn lệch)");
                        UtilReportOMS.RptManager.showReportToWin(this, rpt,strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" },"NPC_ATM");
                    }
                }
                 }
                else if(cboloaidsoat.SelectedIndex==1) //In những hóa đơn khớp giữa 2 bên
                {
                    rptDoisoatNganhang rpt = new rptDoisoatNganhang();
                    rpt.Subreports[1].SetDataSource(ds_KHNOPTIEN_KHOP.Tables["DSKHOP_NPC"]);
                    rpt.Subreports[0].SetDataSource(ds_KHNOPTIEN_KHOP.Tables["DSKHOP_NH"]);
                    rpt.SetParameterValue("pdenngay", pdenngay);
                    rpt.SetParameterValue("ptungay", ptungay);
                    rpt.SetParameterValue("loaidoichieu", "Danh sách những hóa đơn khớp giữa 2 bên");
                    UtilReportOMS.RptManager.showReportToWin(this, rpt, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "NPC_ATM");
                }
                else if (cboloaidsoat.SelectedIndex == 2) //In tat ca hoa don cua 2 ben
                {
                    rptDoisoatNganhang rpt = new rptDoisoatNganhang();
                    rpt.Subreports[1].SetDataSource(ds_KHNOPTIEN_NPC_All.Tables["DSTHU"]);
                    rpt.Subreports[0].SetDataSource(ds_KHNOPTIEN_NH_All.Tables[0]);
                    rpt.SetParameterValue("pdenngay", ptungay);
                    rpt.SetParameterValue("ptungay", ptungay);
                    rpt.SetParameterValue("loaidoichieu", "");
                    UtilReportOMS.RptManager.showReportToWin(this, rpt, strMadviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "NPC_ATM");
                }

            }
            catch
            { 
                return; 
            }

        }

        private void btnDoisoat_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.Wait;
                ptungay = dtpngaynopdoichieu_tu.SelectedDate;
                pdenngay = dtpngaynopdoichieu_den.SelectedDate;

                #region checkngay
                if (pdenngay == null)
                    showMessErrorOnView("Điều kiện ngày nộp không được để trống", dtpngaynop_den);
                if (ptungay == null)
                    showMessErrorOnView("Điều kiện ngày nộp không được để trống", dtpngaynop_tu);
                if (ptungay > pdenngay)
                    showMessErrorOnView("Điều kiện ngày nộp không hợp lệ", dtpngaynop_tu);
                #endregion

                TimeSpan ts = new TimeSpan(i16Hour, i16Munite, 0);
                ptungay = ptungay.Value.AddDays(-1) + ts;
                pdenngay = pdenngay + ts;
                DataTable dt = new DataTable();

                if (txtchonfile.Text.Length == 0)
                {
                    showMessErrorOnView("Bạn chưa chọn file của Ngân hàng để đối chiếu", txtchonfile);
                    return;
                }

                if (ds_KHNOPTIEN_NH != null)
                { ds_KHNOPTIEN_NH.Tables.Clear(); }
                if (ds_KHNOPTIEN_NPC != null)
                { ds_KHNOPTIEN_NPC.Tables.Clear(); }
                if (ds_KHNOPTIEN_NPC_All != null)
                { ds_KHNOPTIEN_NPC_All.Tables.Clear(); }
                if (ds_KHNOPTIEN_NH_All != null)
                { ds_KHNOPTIEN_NH_All.Tables.Clear(); }
                if (ds_KHNOPTIEN_KHOP != null)
                { ds_KHNOPTIEN_KHOP.Tables.Clear(); }

                ds_KHNOPTIEN_NH = ShowFileExcel(txtchonfile.Text.Trim());
                cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //ds_KHNOPTIEN_NPC = bus.get_dsKH_thuATM(strMadviqly, "", "", ptungay, pdenngay, 2);//int_ttrang_no=2: tim ca cham no va chua cham

                ds_KHNOPTIEN_NPC = GlobalServices.Service_NPCUS.get_dsKH_thuATM(strMadviqly, "", "", ptungay, pdenngay, 2);//int_ttrang_no=2: tim ca cham no va chua cham
                if (ds_KHNOPTIEN_NPC == null)
                {
                    showMessErrorOnView("không lấy đựơc thông tin hóa đơn NPC để  đối chiếu", dtpngaynopdoichieu_tu);
                    return;
                }
                if (ds_KHNOPTIEN_NPC != null && ds_KHNOPTIEN_NPC.Tables["DSTHU"].Rows.Count <= 0)
                {
                    showMessErrorOnView("không lấy đựơc thông tin hóa đơn NPC để  đối chiếu", dtpngaynopdoichieu_tu);
                    return;
                }
                if (ds_KHNOPTIEN_NH == null || ds_KHNOPTIEN_NH.Tables[0].Rows.Count <= 0)
                {
                    showMessErrorOnView("không lấy đựơc thông tin hóa đơn ngân hàng để  đối chiếu", dtpngaynopdoichieu_tu);
                    return;
                }
                // Xoa bo nhung dong khong lien quan
                for (Int32 i1 = 0; i1 < ds_KHNOPTIEN_NH.Tables[0].Rows.Count; i1++)
                {
                    if (ds_KHNOPTIEN_NH.Tables[0].Rows[i1]["F3"].ToString().Trim().Length == 0 || ds_KHNOPTIEN_NH.Tables[0].Rows[i1]["F3"].ToString() == "Số hoá đơn")
                    {
                        ds_KHNOPTIEN_NH.Tables[0].Rows.Remove(ds_KHNOPTIEN_NH.Tables[0].Rows[i1]);
                        i1 = i1 - 1;
                    }

                }

                if (ds_KHNOPTIEN_NH.Tables[0].Rows.Count <= 0)
                {
                    showMessErrorOnView("Không có thông tin hóa đơn phía ngân hàng để đối chiếu", dtpngaynopdoichieu_tu);
                    return;
                }
                // Hết phân xoa bo nhung dong khong lien quan

                if (ds_KHNOPTIEN_NPC.Tables["DSTHU"].Rows.Count > 0 && ds_KHNOPTIEN_NH.Tables[0].Rows.Count > 0)
                {
                    DataTable dtNPCall = new DataTable();
                    DataTable dtNHall = new DataTable();
                    DataTable dtNHkhop = ds_KHNOPTIEN_NH.Tables[0].Clone();
                    DataTable dtNPCkhop = ds_KHNOPTIEN_NPC.Tables["DSTHU"].Clone();
                    dtNHkhop.TableName = "DSKHOP_NH";
                    dtNPCkhop.TableName = "DSKHOP_NPC";
                    dtNPCall = ds_KHNOPTIEN_NPC.Tables["DSTHU"].Copy();
                    dtNHall = ds_KHNOPTIEN_NH.Tables[0].Copy();
                    ds_KHNOPTIEN_NPC_All.Tables.Add(dtNPCall);
                    ds_KHNOPTIEN_NH_All.Tables.Add(dtNHall);
                    ds_KHNOPTIEN_NH_All.AcceptChanges();
                    ds_KHNOPTIEN_NPC_All.AcceptChanges();
                    for (int i = 0; i < ds_KHNOPTIEN_NPC.Tables["DSTHU"].Rows.Count; i++)
                    {
                        for (int j = 0; j < ds_KHNOPTIEN_NH.Tables[0].Rows.Count; j++)
                        {
                            if (ds_KHNOPTIEN_NPC.Tables["DSTHU"].Rows[i]["MA_KHANG"].ToString() == ds_KHNOPTIEN_NH.Tables[0].Rows[j]["F4"].ToString() && Convert.ToInt64(ds_KHNOPTIEN_NPC.Tables["DSTHU"].Rows[i]["TTIEN_NOP"]) == Convert.ToInt64(ds_KHNOPTIEN_NH.Tables[0].Rows[j]["F8"]))
                            {
                                dtNHkhop.ImportRow(ds_KHNOPTIEN_NH.Tables[0].Rows[j]);
                                dtNPCkhop.ImportRow(ds_KHNOPTIEN_NPC.Tables["DSTHU"].Rows[i]);
                                ds_KHNOPTIEN_NH.Tables[0].Rows.Remove(ds_KHNOPTIEN_NH.Tables[0].Rows[j]);
                                j = j - 1;
                            }
                        }
                        ds_KHNOPTIEN_NPC.Tables["DSTHU"].Rows.Remove(ds_KHNOPTIEN_NPC.Tables["DSTHU"].Rows[i]);
                        i = i - 1;
                    }
                    if (dtNHkhop.Rows.Count > 0)
                    {
                        ds_KHNOPTIEN_KHOP.Tables.Add(dtNHkhop);
                        ds_KHNOPTIEN_KHOP.Tables.Add(dtNPCkhop);
                        ds_KHNOPTIEN_KHOP.AcceptChanges();
                    }
                    ds_KHNOPTIEN_NH.AcceptChanges();
                    ds_KHNOPTIEN_NPC.AcceptChanges();
                    if (ds_KHNOPTIEN_NPC.Tables["DSTHU"].Rows.Count == 0 && ds_KHNOPTIEN_NH.Tables[0].Rows.Count == 0)
                    {
                        showMessSucOnView("Không có thông tin sai lệch giữa 2 bảng kê");
                        btnindoisoat.IsEnabled = false;
                    }
                    else
                    {
                        showMessSucOnView("Có sai lệch giữa 2 biên bản. Hãy bấm In đối soát để in");
                        btnindoisoat.IsEnabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                showMessErrorOnView(ex.ToString());
            }
            this.Cursor = Cursors.Arrow;
        }

        private void tabThongtinKHnoptien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (tabBKnoptien.IsSelected == true)
                {
                    btnload.IsEnabled = true;
                    btnindoisoat.IsEnabled = false;
                    btnin.IsEnabled = true;
                }
                if (tabDoichieu.IsSelected == true)
                {
                    btnin.IsEnabled = false;
                    btnindoisoat.IsEnabled = true;
                }
            }
            catch
            { }
        }

        private void cmbtinhtrang_cto_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                switch (cbotinhtrang_no.SelectedIndex)
                {
                    case 0:
                        int_ttrang_no = 0;
                        break;
                    case 1:
                        int_ttrang_no = 1;
                        break;
                    case 2:
                        int_ttrang_no = 2;
                        break;
                    default:
                        int_ttrang_no = 0;
                        break;
                }
            }
            catch { }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string ERRO = "";
                DataSet dsCancel = new DataSet();
                DataTable dtCancel = new DataTable();
                dtCancel = ds_KHNOPTIEN.Tables["DSTHU"].Clone();
                dtCancel.TableName = "HUYCHAM";
                DataView drv = dgrdsthutien.ItemsSource as DataView;
                    DataRow[] drselect = drv.Table.Select("CHON=1");
                    if (drselect.Length > 0)
                    {
                        if (MessageBox.Show("Bạn có muốn hủy chấm nợ cho những hóa đơn đã được chọn?", "Thông báo", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                        {
                            foreach (DataRow dr in drselect)
                            {
                                if (Convert.ToInt64(dr["TIEN_NO"].ToString()) == 0 && Convert.ToInt64(dr["SO_TIEN"].ToString()) != 0)
                                {
                                    //Can bo xung kiem tra ngay nop && thang HT
                                    dtCancel.ImportRow(dr);
                                }
                            }
                            dtCancel.AcceptChanges();
                            dsCancel.Tables.Add(dtCancel);
                        }
                    }
                    if (dsCancel.Tables["HUYCHAM"].Rows.Count > 0)
                    {
                        //bus_TienichNPC.cls_TienichNPC_bus bus2 = new cls_TienichNPC_bus();
                        //if (bus2.cancel_chamno_atm(dsCancel))
                        if (GlobalServices.Service_NPCUS.cancel_chamno_atm(dsCancel,strUsername,ref ERRO))
                        {
                            MessageBox.Show("Hủy chấm nợ thành công");
                            loadgrid();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Lỗi hủy chấm nợ");
                            return;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Không có hóa đơn để hủy chấm");
                        return;
                    }

            }
            catch(Exception ex)
            { 
                showMessErrorOnView(ex.ToString());
            }
        }

        private void dgrdsthutien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        public void loadgrid()
        { 
            try
            {
                ds_KHNOPTIEN.Clear();
                ds_KHNOPTIEN = GlobalServices.Service_NPCUS.get_dsKH_thuATM(strMadviqly, strmanh, strmapttt, ptungay, pdenngay, int_ttrang_no);
                if (ds_KHNOPTIEN != null)
                {
                    if (ds_KHNOPTIEN.Tables.Count > 0)
                    {
                        if (ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count > 0)
                        {
                            dgrdsthutien.ItemsSource = ds_KHNOPTIEN.Tables[0].DefaultView;
                            ds_KHNOPTIEN.Tables[0].DefaultView.AllowNew = false;
                            if (int_ttrang_no == 0)
                            {
                                this.btnGhi.IsEnabled = true;
                                btnDelete.IsEnabled = false;
                            }
                            if (int_ttrang_no == 1)
                            {
                                this.btnGhi.IsEnabled = false;
                                btnDelete.IsEnabled = true;
                            }
                            if (int_ttrang_no == 2)
                            {
                                this.btnGhi.IsEnabled = false;
                                btnDelete.IsEnabled = false;
                            }
                        }
                    }
                }
            
                for (Int32 i = 0; i < ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count; i++)
                {
                    ptien = ptien + Convert.ToInt64(ds_KHNOPTIEN.Tables["DSTHU"].Rows[i]["TTIEN_NOP"]);
                }
                ptcount = ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count;
                pcount = ds_KHNOPTIEN.Tables["DSTHU"].Rows.Count;
                lblTongSo.Content = "Tổng số:" + pcount.ToString() + "/" + ptcount;
                lblTongtien.Content = "Tổng tiền:" + ptien;
                btnHuy.IsEnabled = true;
                btnin.IsEnabled = true;
            }
            catch
            {}
        }

 
        private void txtTimKiemMA_KHANG_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                string s = txtTimKiemMA_KHANG.Text;
                if (s.Trim() == "")
                {
                    dgrdsthutien.SelectedIndex = 0;
                    return;
                }
                DataTable dt = ds_KHNOPTIEN.Tables["DSTHU"] as DataTable;
                DataRow[] dr = dt.Select("MA_KHANG LIKE '%" + s.ToUpper() + "%'", "MA_KHANG");
                if (dr.Count() == 0)
                {
                    dgrdsthutien.SelectedIndex = 0;
                    return;
                }
                else
                {
                    for (int i = 0; i < dgrdsthutien.Items.Count; i++)
                    {
                        DataGridRow row = (DataGridRow)dgrdsthutien.ItemContainerGenerator.ContainerFromIndex(i);
                        TextBlock cellContent = dgrdsthutien.Columns[3].GetCellContent(row) as TextBlock;
                        if (cellContent != null && cellContent.Text.ToUpper().Contains(txtTimKiemMA_KHANG.Text.ToUpper()))
                        {
                            object item = dgrdsthutien.Items[i];
                            dgrdsthutien.SelectedItem = item;
                            dgrdsthutien.ScrollIntoView(item);
                            row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                            txtTimKiemMA_KHANG.Focus();
                            break;
                        }
                    }

                }
            }
            catch (Exception ex)
            { showMessErrorOnView(ex.ToString()); }
        }

        private void txtTimKiemMA_KHANG_KeyDown(object sender, KeyEventArgs e)
        {

            string s = txtTimKiemMA_KHANG.Text;
            if (s.Trim() == "")
            {
                txtTimKiemMA_KHANG.Focus();
                //e.Handled = true;
                return;
            }
            int Itemrow = dgrdsthutien.SelectedIndex;
            DataRowView drw = null;
            if (e.Key == Key.Enter)
            {
                drw = (DataRowView)dgrdsthutien.Items[Itemrow];
                if (Convert.ToBoolean(drw["CHON"]) == false)
                {
                    drw["CHON"] = 1;
                    pcount = pcount + 1;
                    ptien = ptien + Convert.ToInt64(drw["TTIEN_NOP"].ToString());
                }
                else
                {
                    drw["CHON"] = 0;
                    pcount = pcount - 1;
                    ptien = ptien - Convert.ToInt64(drw["TTIEN_NOP"].ToString());
                }
                lblTongSo.Content = "Tổng số:" + pcount.ToString() + "/" + ptcount;
                lblTongtien.Content = "Tổng tiền:" + ptien;
                e.Handled = true;
                txtTimKiemMA_KHANG.Focus();
                return;
            }
        }

        private void txtTimKiemID_HDON_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                string s = txtTimKiemID_HDON.Text;
                if (s.Trim() == "")
                {
                    dgrdsthutien.SelectedIndex = 0;
                    return;
                }
                DataTable dt = ds_KHNOPTIEN.Tables["DSTHU"] as DataTable;
                DataRow[] dr = dt.Select("ID_HDON = '" + s.ToUpper() + "'", "ID_HDON");
                if (dr.Count() == 0)
                {
                    dgrdsthutien.SelectedIndex = 0;
                    return;
                }
                else
                {
                    for (int i = 0; i < dgrdsthutien.Items.Count; i++)
                    {
                        DataGridRow row = (DataGridRow)dgrdsthutien.ItemContainerGenerator.ContainerFromIndex(i);
                        TextBlock cellContent = dgrdsthutien.Columns[9].GetCellContent(row) as TextBlock;
                        if (cellContent != null && cellContent.Text.ToUpper().Contains(txtTimKiemID_HDON.Text.ToUpper()))
                        {
                            object item = dgrdsthutien.Items[i];
                            dgrdsthutien.SelectedItem = item;
                            dgrdsthutien.ScrollIntoView(item);
                            row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                            txtTimKiemID_HDON.Focus();
                            break;
                        }
                    }

                }
            }
            catch (Exception ex)
            { showMessErrorOnView(ex.ToString()); }
        }

        private void txtTimKiemID_HDON_KeyDown(object sender, KeyEventArgs e)
        {
            string s = txtTimKiemID_HDON.Text;
            if (s.Trim() == "")
            {
                txtTimKiemID_HDON.Focus();
                //e.Handled = true;
                return;
            }
            int Itemrow = dgrdsthutien.SelectedIndex;
            DataRowView drw = null;
            if (e.Key == Key.Enter)
            {
                drw = (DataRowView)dgrdsthutien.Items[Itemrow];
                if (Convert.ToBoolean(drw["CHON"]) == false)
                {
                    drw["CHON"] = 1;
                    pcount = pcount + 1;
                    ptien = ptien + Convert.ToInt64(drw["TTIEN_NOP"].ToString());
                }
                else
                {
                    drw["CHON"] = 0;
                    pcount = pcount - 1;
                    ptien = ptien - Convert.ToInt64(drw["TTIEN_NOP"].ToString());
                }
                lblTongSo.Content = "Tổng số:" + pcount.ToString() + "/" + ptcount;
                lblTongtien.Content = "Tổng tiền:" + ptien;
                e.Handled = true;
                txtTimKiemID_HDON.Focus();
                return;
            }
        }
        //private void resetLblStatus(object componert)
        //{
        //    if (componert != null)
        //    {
        //        if (componert is IValidate)
        //        {
        //            ((IValidate)componert).SetCurrentBackGround();
        //        }
        //    }
        //    //Timer.Enabled = false;            
        //    lblstatus.Content = "";
        //    lblstatus.Visibility = Visibility.Visible;
        //}
    }
}
